package nezumi.gui;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

/**
 * A sprite to be displayed on the screen. Note that a sprite contains no state
 * information, i.e. its just the image and not the location. This allows us to
 * use a single sprite in lots of different places without having to store
 * multiple copies of the image.
 * 
 * @author rui
 */
public class Sprite {
	/** The image to be drawn for this sprite */
	private final Image image;

	/**
	 * Create a new sprite based on an image
	 * 
	 * @param image
	 *            The image that is this sprite
	 */
	public Sprite(Image image) {
		this.image = image;
	}

	/**
	 * Get the width of the drawn sprite
	 * 
	 * @return The width in pixels of this sprite
	 */
	public int getWidth() {
		return image.getWidth(null);
	}

	/**
	 * Get the height of the drawn sprite
	 * 
	 * @return The height in pixels of this sprite
	 */
	public int getHeight() {
		return image.getHeight(null);
	}

	/**
	 * Draw the sprite centered at (x,y)
	 * 
	 * @param g
	 * @param x
	 * @param y
	 */
	public void drawCentered(Graphics2D g, int x, int y) {

		double xshift = image.getWidth(null) / 2;
		double yshift = image.getHeight(null) / 2;
		
		AffineTransform at = AffineTransform.getTranslateInstance(x - xshift, y
				- yshift);

		g.drawImage(image, at, null);
	}

	/**
	 * Draw the sprite onto the provided graphics context while applying a.
	 * 
	 * @param g
	 * @param x
	 * @param y
	 * @param rot -
	 *            rotation angle in radians
	 */
	public void drawCentered(Graphics2D g, int x, int y, double rot, Rectangle bounds) {

		drawCentered(g, x, y);

		// final int imageWidth = image.getWidth(null), imageHeight =
		// image.getHeight(null);

		// creating the AffineTransform instance
		// AffineTransform affineTransform = new AffineTransform();
		// set the translation to the mid of the component
		// affineTransform.setToTranslation((bounds.width-imageWidth)/2,(bounds.height-imageHeight)/2);
		// rotate with the anchor point as the mid of the image
		// affineTransform.rotate(rot, imageWidth/2, imageHeight/2);

		// translate to final position
		// affineTransform.translate(x, y);

		// draw the image using the AffineTransform
		// g.drawImage(image, affineTransform, null);

	}

}